﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;

namespace INSAForManagers
{
    public partial class FormEEvalueElement : Form
    {
        private DataGridViewRow dgvRow;
        private int checkCount = 0;
        private Decimal originItemValue;

        public FormEEvalueElement()
        {
            InitializeComponent();
        }

        public FormEEvalueElement(DataGridViewRow dgvRow)
        {
            InitializeComponent();

            this.dgvRow = dgvRow;
            
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void numItem1_ValueChanged(object sender, EventArgs e)
        {
            try
            {                

                if (Config.EnabledListViewEvent == true)
                {
                    double item1 = Double.Parse(numItem1.Value.ToString());
                    double item2 = Double.Parse(numItem2.Value.ToString());
                    double item3 = Double.Parse(numItem3.Value.ToString());
                    double item4 = Double.Parse(numItem4.Value.ToString());
                    double item5 = Double.Parse(numItem5.Value.ToString());

                    double total = item1 + item2 + item3 + item4 + item5;
                    double originTotal = Double.Parse(txtTotal.Text);
                    double diff = originTotal - total;
                    double result = double.Parse(selectedItem(1).Value.ToString()) + diff;

                    Config.EnabledListViewEvent = false;
                    selectedItem(1).Value = Decimal.Parse(result.ToString());
                    Config.EnabledListViewEvent = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "확인");
            }
        }

        private void numItem2_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (Config.EnabledListViewEvent == true)
                {
                    double item1 = Double.Parse(numItem1.Value.ToString());
                    double item2 = Double.Parse(numItem2.Value.ToString());
                    double item3 = Double.Parse(numItem3.Value.ToString());
                    double item4 = Double.Parse(numItem4.Value.ToString());
                    double item5 = Double.Parse(numItem5.Value.ToString());

                    double total = item1 + item2 + item3 + item4 + item5;
                    double originTotal = Double.Parse(txtTotal.Text);
                    double diff = originTotal - total;
                    double result = double.Parse(selectedItem(2).Value.ToString()) + diff;

                    Config.EnabledListViewEvent = false;
                    selectedItem(2).Value = Decimal.Parse(result.ToString());
                    Config.EnabledListViewEvent = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "확인");
            }
        }

        private void numItem3_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (Config.EnabledListViewEvent == true)
                {
                    double item1 = Double.Parse(numItem1.Value.ToString());
                    double item2 = Double.Parse(numItem2.Value.ToString());
                    double item3 = Double.Parse(numItem3.Value.ToString());
                    double item4 = Double.Parse(numItem4.Value.ToString());
                    double item5 = Double.Parse(numItem5.Value.ToString());

                    double total = item1 + item2 + item3 + item4 + item5;
                    double originTotal = Double.Parse(txtTotal.Text);
                    double diff = originTotal - total;
                    double result = double.Parse(selectedItem(3).Value.ToString()) + diff;

                    Config.EnabledListViewEvent = false;
                    selectedItem(3).Value = Decimal.Parse(result.ToString());
                    Config.EnabledListViewEvent = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "확인");
            }
        }

        private void numItem4_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (Config.EnabledListViewEvent == true)
                {
                    double item1 = Double.Parse(numItem1.Value.ToString());
                    double item2 = Double.Parse(numItem2.Value.ToString());
                    double item3 = Double.Parse(numItem3.Value.ToString());
                    double item4 = Double.Parse(numItem4.Value.ToString());
                    double item5 = Double.Parse(numItem5.Value.ToString());

                    double total = item1 + item2 + item3 + item4 + item5;
                    double originTotal = Double.Parse(txtTotal.Text);
                    double diff = originTotal - total;
                    double result = double.Parse(selectedItem(4).Value.ToString()) + diff;

                    Config.EnabledListViewEvent = false;
                    selectedItem(4).Value = Decimal.Parse(result.ToString());
                    Config.EnabledListViewEvent = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "확인");
            }
        }

        private void numItem5_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                if (Config.EnabledListViewEvent == true)
                {
                    double item1 = Double.Parse(numItem1.Value.ToString());
                    double item2 = Double.Parse(numItem2.Value.ToString());
                    double item3 = Double.Parse(numItem3.Value.ToString());
                    double item4 = Double.Parse(numItem4.Value.ToString());
                    double item5 = Double.Parse(numItem5.Value.ToString());

                    double total = item1 + item2 + item3 + item4 + item5;
                    double originTotal = Double.Parse(txtTotal.Text);
                    double diff = originTotal - total;
                    double result = double.Parse(selectedItem(5).Value.ToString()) + diff;

                    Config.EnabledListViewEvent = false;
                    selectedItem(5).Value = Decimal.Parse(result.ToString());
                    Config.EnabledListViewEvent = true;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "확인");
            }
        }

        private void FormEEvalueElement_Load(object sender, EventArgs e)
        {
            Config.EnabledListViewEvent = false;

            lblName.Text = dgvRow.Cells[3].Value.ToString();

            numItem1.Value = Decimal.Parse(dgvRow.Cells[6].Value.ToString());
            numItem2.Value = Decimal.Parse(dgvRow.Cells[7].Value.ToString());
            numItem3.Value = Decimal.Parse(dgvRow.Cells[8].Value.ToString());
            numItem4.Value = Decimal.Parse(dgvRow.Cells[9].Value.ToString());
            numItem5.Value = Decimal.Parse(dgvRow.Cells[10].Value.ToString());

            txtTotal.Text = Double.Parse(dgvRow.Cells[11].Value.ToString()).ToString("#0.000");

            Config.EnabledListViewEvent = true;

            numItem1.Enabled = false;
            numItem2.Enabled = false;
            numItem3.Enabled = false;
            numItem4.Enabled = false;
            numItem5.Enabled = false;
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            dgvRow.Cells[6].Value = numItem1.Value;
            dgvRow.Cells[7].Value = numItem2.Value;
            dgvRow.Cells[8].Value = numItem3.Value;
            dgvRow.Cells[9].Value = numItem4.Value;
            dgvRow.Cells[10].Value = numItem5.Value;

            this.Close();
        }

        private void rdoStateChange(int i)
        {
            if (i == 1)
            {
                rdoStateRecover();
                rdoItem1.Checked = false;
                rdoItem1.Enabled = false;
                numItem1.Enabled = true;
            }
            else if (i == 2)
            {
                rdoStateRecover();
                rdoItem2.Checked = false;
                rdoItem2.Enabled = false;
                numItem2.Enabled = true;
            }
            else if (i == 3)
            {
                rdoStateRecover();
                rdoItem3.Checked = false;
                rdoItem3.Enabled = false;
                numItem3.Enabled = true;
            }
            else if (i == 4)
            {
                rdoStateRecover();
                rdoItem4.Checked = false;
                rdoItem4.Enabled = false;
                numItem4.Enabled = true;
            }
            else if (i == 5)
            {
                rdoStateRecover();
                rdoItem5.Checked = false;
                rdoItem5.Enabled = false;
                numItem5.Enabled = true;
            }
        }

        private void rdoStateRecover()
        {
            rdoItem1.Checked = false;
            rdoItem1.Enabled = true;

            numItem1.Enabled = false;

            rdoItem2.Checked = false;
            rdoItem2.Enabled = true;

            numItem2.Enabled = false;

            rdoItem3.Checked = false;
            rdoItem3.Enabled = true;

            numItem3.Enabled = false;

            rdoItem4.Checked = false;
            rdoItem4.Enabled = true;

            numItem4.Enabled = false;

            rdoItem5.Checked = false;
            rdoItem5.Enabled = true;

            numItem5.Enabled = false;
        }

        private NumericUpDown selectedItem(int i)
        {
            NumericUpDown result = null;

            if (rdoItem1.Checked == true && i != 1)
            {
                result = numItem1;
            }
            else if (rdoItem2.Checked == true && i != 2)
            {
                result = numItem2;
            }
            else if (rdoItem3.Checked == true && i != 3)
            {
                result = numItem3;
            }
            else if (rdoItem4.Checked == true && i != 4)
            {
                result = numItem4;
            }
            else if (rdoItem5.Checked == true && i != 5)
            {
                result = numItem5;
            }

            return result;
        }

        private void rdoItem1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkCount > 2 && rdoItem1.Checked == true)
            {
                rdoItem1.Checked = false;
                numItem1.Enabled = rdoItem1.Checked;
                checkCount--;
                return;
            }
            else if (checkCount > 2 && rdoItem1.Checked == false)
            {
                rdoItem1.Checked = false;
                numItem1.Enabled = rdoItem1.Checked;
                return;
            }

            numItem1.Enabled = rdoItem1.Checked;
            originItemValue = numItem1.Value;
            checkCount++;
        }

        private void rdoItem2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkCount > 2 && rdoItem2.Checked == true)
            {
                rdoItem2.Checked = false;
                numItem2.Enabled = rdoItem1.Checked;
                checkCount--;
                return;
            }
            else if (checkCount > 2 && rdoItem2.Checked == false)
            {
                rdoItem2.Checked = false;
                numItem2.Enabled = rdoItem2.Checked;
                return;
            }

            numItem2.Enabled = rdoItem2.Checked;
            originItemValue = numItem2.Value;
            checkCount++;
        }

        private void rdoItem3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkCount > 2 && rdoItem3.Checked == true)
            {
                rdoItem3.Checked = false;
                numItem3.Enabled = rdoItem1.Checked;
                checkCount--;
                return;
            }
            else if (checkCount > 2 && rdoItem3.Checked == false)
            {
                rdoItem3.Checked = false;
                numItem3.Enabled = rdoItem3.Checked;
                return;
            }

            numItem3.Enabled = rdoItem3.Checked;
            originItemValue = numItem3.Value;
            checkCount++;
        }

        private void rdoItem4_CheckedChanged(object sender, EventArgs e)
        {
            if (checkCount > 2 && rdoItem4.Checked == true)
            {
                rdoItem4.Checked = false;
                numItem4.Enabled = rdoItem4.Checked;
                checkCount--;
                return;
            }
            else if (checkCount > 2 && rdoItem4.Checked == false)
            {
                rdoItem4.Checked = false;
                numItem4.Enabled = rdoItem4.Checked;
                return;
            }

            numItem4.Enabled = rdoItem4.Checked;
            originItemValue = numItem4.Value;
            checkCount++;
        }

        private void rdoItem5_CheckedChanged(object sender, EventArgs e)
        {
            if (checkCount > 2 && rdoItem5.Checked == true)
            {
                rdoItem5.Checked = false;
                numItem5.Enabled = rdoItem5.Checked;
                checkCount--;
                return;
            }
            else if (checkCount > 2 && rdoItem5.Checked == false)
            {
                rdoItem5.Checked = false;
                numItem5.Enabled = rdoItem5.Checked;
                return;
            }

            numItem5.Enabled = rdoItem5.Checked;
            originItemValue = numItem5.Value;
            checkCount++;
        }
    }
}
